﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using SKYPE4COMLib;
using MediaPortal.GUI.Library;


namespace Skype4MP
{
  public class SkypeVideo
  {
    // Variable etc declarations
    #region Declarations

    static Bitmap bmSkype;
    static Bitmap bmSkypeCrop;
    public static IntPtr hSkypeMain;
    static IntPtr hSkypeVideo;
    static IntPtr hSkypeVideo1;
    static IntPtr hSkypeVideo2;
    public static IntPtr hSkypeLiveButton;
    static System.Windows.Forms.Timer VideoTimer;
    static System.Windows.Forms.Timer WindowTimer;
    static System.Drawing.Size size;
    static GUIImage sVideo;
    static float timePassed;
    static int sFrame;
    static String sTextureName = string.Empty;
    static String oldsTextureName = string.Empty;
   

    #endregion


    // Constants
    #region Constants
    // All constants moved to SkypeHelper
    #endregion

    // DLL imports
    #region Imports
    // All imports moved to SkypeHelper except these, does not work otherwise
    [DllImport("User32.dll")]
    public static extern Boolean EnumChildWindows(IntPtr hwnd, CallBack x, IntPtr y);
    public delegate bool CallBack(IntPtr hwnd, IntPtr lParam);
    #endregion

    #region tests
    public static string GethSkypeMain() { return "" + hSkypeMain; }
    public static string GethSkypeVideo() { return "" + hSkypeVideo; }
    public static string GethSkypeVideo1() { return "" + hSkypeVideo1; }
    public static string GethSkypeVideo2() { return "" + hSkypeVideo2; }

    #endregion

    public static void StartVideoCapture(GUIImage SkypeVideo, IntPtr hMP)
    {
      hSkypeMain = SkypeHelper.SkypeWin32.FindWindow("tSkMainForm.UnicodeClass", null);
      SkypeHelper.SkypeWin32.PostMessage(hSkypeMain, SkypeHelper.WM_SYSKEYDOWN, SkypeHelper.SkypeWin32.VkKeyScan('2'), 1 << 29);
      SkypeHelper.SkypeWin32.PostMessage(hSkypeMain, SkypeHelper.WM_SYSKEYDOWN, SkypeHelper.SkypeWin32.VkKeyScan('1'), 1 << 29);

      SkypeHelper.SkypeWin32.ShowWindow(hSkypeMain, SkypeHelper.SW_SHOWMAXIMIZED);
      SkypeHelper.SkypeWin32.SetWindowPos(hSkypeMain, hMP, 0, 0, 0, 0, 0);
      SkypeHelper.SkypeWin32.UpdateWindow(hSkypeMain);
      hSkypeVideo = IntPtr.Zero;

      sVideo = SkypeVideo;
      timePassed = 0;
      sFrame = 0;
      WindowTimer = new System.Windows.Forms.Timer();
      WindowTimer.Tick += new EventHandler(WindowTimerEventProcessor);
      WindowTimer.Interval = 100;
      WindowTimer.Start();
      VideoTimer = new System.Windows.Forms.Timer();
      VideoTimer.Tick += new EventHandler(TimerEventProcessor);
      VideoTimer.Interval = 50;
      VideoTimer.Start();
    }

    public static void StopVideoCapture()
    {
      VideoTimer.Stop();
      WindowTimer.Stop();
    }

    public static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {
      VideoTimer.Stop();
      SkypeHelper.SkypeWin32.UpdateWindow(hSkypeVideo);
      IntPtr hdc = SkypeHelper.SkypeWin32.GetWindowDC(hSkypeMain);
      IntPtr hdcMem = SkypeHelper.SkypeWin32.CreateCompatibleDC(hdc);

      SkypeHelper.RECT rcSkype = new SkypeHelper.RECT();
      SkypeHelper.RECT rcSkypeVideo = new SkypeHelper.RECT();
      SkypeHelper.SkypeWin32.GetWindowRect(hSkypeMain, out rcSkype);
      SkypeHelper.SkypeWin32.GetWindowRect(hSkypeVideo, out rcSkypeVideo);

      IntPtr m_HBitmap = SkypeHelper.SkypeWin32.CreateCompatibleBitmap(hdc, rcSkype.Right - rcSkype.Left, rcSkype.Bottom - rcSkype.Top);

      SkypeHelper.SkypeWin32.SelectObject(hdcMem, m_HBitmap);
      SkypeHelper.SkypeWin32.PrintWindow(hSkypeMain, hdcMem, 0);

      bmSkype = Image.FromHbitmap(m_HBitmap);
      size.Width = rcSkypeVideo.Right - rcSkypeVideo.Left;
      size.Height = rcSkypeVideo.Bottom - rcSkypeVideo.Top;
      try
      {
        oldsTextureName = sTextureName;
        bmSkypeCrop = bmSkype.Clone(new Rectangle(rcSkypeVideo.Left - rcSkype.Left, rcSkypeVideo.Top - rcSkype.Top, rcSkypeVideo.Right - rcSkypeVideo.Left, rcSkypeVideo.Bottom - rcSkypeVideo.Top), bmSkype.PixelFormat);
        sTextureName = buildMemoryImage(bmSkypeCrop, "Skype" + sFrame, size);
        
        sVideo.FileName = sTextureName;
        sVideo.Render(timePassed);
        GUITextureManager.ReleaseTexture(oldsTextureName);
      }
      catch (Exception)
      {
      }
      SkypeHelper.SkypeWin32.DeleteObject(m_HBitmap);
      SkypeHelper.SkypeWin32.DeleteObject(hdcMem);
      SkypeHelper.SkypeWin32.ReleaseDC(hSkypeMain, hdc);
      sFrame++;
      VideoTimer.Start();
    }

    public static void WindowTimerEventProcessor(Object myObject, EventArgs myEventArgs)
    {
      //WindowTimer.Stop();
      CallBack myCallBack = new CallBack(GetSkypeWindows);
      hSkypeVideo1 = IntPtr.Zero;
      hSkypeVideo2 = IntPtr.Zero;
      EnumChildWindows(hSkypeMain, myCallBack, IntPtr.Zero);
      hSkypeVideo = hSkypeVideo1;
    }

    private static bool GetSkypeWindows(IntPtr hWnd, IntPtr lParam)
    {
      SkypeHelper.SkypeWin32.UpdateWindow(hWnd);
      StringBuilder sb = new StringBuilder(1024);
      SkypeHelper.SkypeWin32.GetClassName(hWnd, sb, sb.Capacity);
      string sClass = sb.ToString().Trim();
      if (sClass == "TLiveConversationList")
      {
        IntPtr llParam = (IntPtr)((10) | 10); // The coordinates
        IntPtr wwParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
        const uint downCode = 0x201; // Left click down code
        const uint upCode = 0x202; // Left click up code
        SkypeHelper.SkypeWin32.SendMessage(hWnd, downCode, wwParam, llParam); // Mouse button down
        SkypeHelper.SkypeWin32.SendMessage(hWnd, upCode, wwParam, llParam); // Mouse button up
      }
      if (sClass == "TCustomVideoWindow")
      {
        if (hWnd != hSkypeVideo1)
        {
          hSkypeVideo1 = hWnd;
        }
        else
        {
          hSkypeVideo2 = hWnd;
        }
      }
      return true;
    }

    private static string buildMemoryImage(System.Drawing.Bitmap image, string identifier, System.Drawing.Size size)
    {
      string name = String.Format("[Skype:{0}]", identifier);
      try
      {
        GUITextureManager.LoadFromMemory(image, name, 0, size.Width, size.Height);
      }
      catch (Exception)
      {
      }
      return name;

    }

  }
}
